package com.example.studywords.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;

import android.widget.TextView;

import com.example.studywords.MainActivity;
import com.example.studywords.R;
import com.example.studywords.bean.TranslateBean;
import com.example.studywords.bean.WordBean;
import com.example.studywords.dlgs.CheckDlg;
import com.example.studywords.sql.MyDatabase;
import com.example.studywords.utils.SqlUtil;
import com.google.android.material.snackbar.Snackbar;

import java.util.List;

public class GridViewAdapter extends BaseAdapter {
    private List<WordBean> words;
    private Context context;
    private View mView;
    private MainActivity activity;
    public GridViewAdapter(List<WordBean> words, Context context){
        this.words = words;
        this.context = context;
    }
    @Override
    public int getCount() {
        return words.size();
    }

    @Override
    public Object getItem(int i) {
        return words.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.view_word_card,null);
        mView = view;
        activity = (MainActivity)mView.getContext();
        TextView wordText = view.findViewById(R.id.cardWordText);
        EditText translateText = view.findViewById(R.id.cardTranslateText);
        Button checkButton = view.findViewById(R.id.cardCheckButton),
                passButton = view.findViewById(R.id.cardPassButton);

        wordText.setText(words.get(i).getWordContent());

        checkButton.setOnClickListener(l->{
            CheckDlg checkDlg = new CheckDlg(words.get(i),translateText.getText().toString());
            checkDlg.show(activity.getSupportFragmentManager(),"check_dlg");
        });
        passButton.setOnClickListener(l->{
            CheckDlg checkDlg = new CheckDlg(words.get(i),translateText.getText().toString());
            checkDlg.show(activity.getSupportFragmentManager(),"check_dlg");
        });
        return view;
    }
    //Snackbar上只能有一个Action!满足不了要求,废弃了
    private void showCheckSnackbar(int i,View view,String inputTranslation){
        new Thread(()->{
            //检查填写的翻译是否正确
            boolean isCorrect = false;
            List<TranslateBean> translations = SqlUtil.selectTranslationByWordId(words.get(i).getWordId(),context);
            //将所有翻译连成一句话
            String result = "";
            for(TranslateBean translation:translations){
                if(translation.getTranslationContent().equals(inputTranslation))
                    isCorrect = true;
                result = result + (translation.getTranslationContent() + " ");
            }
            //显示Snackbar
            if(isCorrect){
                Snackbar.make(view,"正确:"+result,Snackbar.LENGTH_LONG)
                        .setDuration(Snackbar.LENGTH_LONG)
                        .show();
                SqlUtil.updateWordInfo(words.get(i).getWordId(),true,context);
            }else{
                Snackbar.make(view,"错误:"+result,Snackbar.LENGTH_LONG)
                        .setAction("也正确", v->{
                            SqlUtil.insertRelation(words.get(i).getWordContent(),"",inputTranslation,context);
                            SqlUtil.updateWordInfo(words.get(i).getWordId(),true,context);
                        })
                        .setAction("错了",v->{
                            SqlUtil.updateWordInfo(words.get(i).getWordId(),false,context);
                        })
                        .setDuration(Snackbar.LENGTH_LONG)
                        .show();
            }
            removeWordAndRefresh(i);
        }).start();
    }
    //Snackbar上只能有一个Action!满足不了要求,废弃了
    private void showPassSnackbar(int i,View view){
        new Thread(()->{
            List<TranslateBean> translations = SqlUtil.selectTranslationByWordId(words.get(i).getWordId(),context);
            //将所有翻译连成一句话
            String result = "";
            for(TranslateBean translation:translations){
                result+=translation.getTranslationContent()+" ";
            }
            Snackbar.make(view,result,Snackbar.LENGTH_LONG)
                    .setAction("对的",v-> SqlUtil.updateWordInfo(words.get(i).getWordId(),true,context))
                    .setAction("错了",v->SqlUtil.updateWordInfo(words.get(i).getWordId(),false,context))
                    .setDuration(Snackbar.LENGTH_LONG)
                    .show();
            removeWordAndRefresh(i);
        }).start();

    }

    private void removeWordAndRefresh(int i){
        words.remove(i);
        activity.runOnUiThread(this::notifyDataSetChanged);
    }

    public void reReadDataAndRefresh(){
        new Thread(()->{
            words = MyDatabase.getDatabase(context).wordDao().selectAll();
            activity.runOnUiThread(this::notifyDataSetChanged);
        }).start();
    }

    /**
     * 根据条件搜索符合的单词
     * @param searchCondition 查询条件
     */
    public void searchDataAndRefresh(String searchCondition){
        String searchSymbol = searchCondition.split(" ")[0];
        int searchText = Integer.parseInt(searchCondition.split(" ")[1]);

        if(isSearchCorrectTime(searchSymbol)){
            new Thread(()->{
                List<Integer> wordIds =
                MyDatabase.getDatabase(context).wordInfoDao().selectWordWithCorrectNum(searchText);
                words.clear();
                for(int wordId : wordIds){
                    words.add(MyDatabase.getDatabase(context).wordDao().selectById(wordId));
                }
                activity.runOnUiThread(this::notifyDataSetChanged);
            }).start();
        }
        if(isSearchDateDistance(searchSymbol)){
            new Thread(()->{
                //TODO:这个需求还没想明白,先用着吧...
            }).start();
        }
    }
    private boolean isSearchCorrectTime(String searchSymbol){
        return searchSymbol.equals("t")||searchSymbol.equals("T");
    }
    private boolean isSearchDateDistance(String searchSymbol){
        return searchSymbol.equals("d")||searchSymbol.equals("D");
    }


}
